home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / CITADEL BBS 'C' SRC / NETRCV.C < prev    next >
C/C++ Source or Header  |  1987-01-14  |  16KB  |  545 lines

  1. /************************************************************************/
  2. /*                netrcv.c                */
  3. /*                                    */
  4. /*    Networking functions for reception.                */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /*                history                 */
  9. /*                                    */
  10. /* 86Aug20 HAW    History not maintained due to space problems.        */
  11. /************************************************************************/
  12.  
  13. #include "ctdl.h"
  14.  
  15. /************************************************************************/
  16. /*                contents                */
  17. /*                                    */
  18. /************************************************************************/
  19.  
  20. /************************************************************************/
  21. /*           External variable declarations in NET.C        */
  22. /************************************************************************/
  23. char        processMail;
  24. #ifdef NET_BUG
  25. extern FILE *localcheck;
  26. #endif
  27. extern FILE        *netLog, *netMisc;
  28. extern ulong        roomHiMsgs[MAXROOMS];
  29. extern unsigned char    sectBuf[SECTSIZE + 5];
  30. extern int        counter;
  31. extern int        callSlot;
  32. extern char        checkNegMail;
  33. extern char        inReceive;
  34. extern int        FinHour, FinMinute;
  35. extern label        normed, callerName, callerId;
  36.  
  37. char         normId(), getNetMessage();
  38. char         callOut();
  39. unsigned char     inp();
  40.  
  41. char *netRoomTemplate = "%c:room%d.$$$";
  42.  
  43. extern struct nodeRoomsTab *sharedRooms;
  44.  
  45. extern struct config    cfg;        /* Lots an lots of variables    */
  46. extern struct logBuffer logBuf;     /* Person buffer        */
  47. extern struct aRoom    roomBuf;    /* Room buffer            */
  48. extern struct rTable    roomTab[];
  49. extern struct msgB    msgBuf,tempMess;
  50. extern struct netBuffer netBuf;
  51. extern struct netTable    *netTab;
  52. extern FILE        *upfd;
  53. extern int        thisNet;
  54. extern char        onConsole;
  55. extern char        loggedIn;    /* Is we logged in?        */
  56. extern char        outFlag;    /* Output flag            */
  57. extern char        haveCarrier;    /* Do we still got carrier?    */
  58. extern unsigned char    modStat;    /* Needed so we don't die    */
  59. extern char        usingWCprotocol;/* WC flag            */
  60. extern char        WCError;
  61. extern int        thisRoom;
  62. extern int        thisLog;
  63. extern char        *dotdot;
  64. extern char        *confirm;
  65.  
  66. /************************************************************************/
  67. /*           External function definitions for NET.C        */
  68. /************************************************************************/
  69. FILE *safeopen();
  70. FILE *fopen();
  71. char *realloc();
  72. char *index();
  73. long getNumber();
  74.  
  75. /************************************************************************/
  76. /*    called() We've been called, so let's handle it            */
  77. /************************************************************************/
  78. called()
  79. {
  80.     int         count;
  81.     int         yr, hr, min, dy;
  82.     char        *mn;
  83.  
  84.     inReceive = TRUE;
  85.     getdate(&yr, &mn, &dy, &hr, &min);
  86.     splitF(netLog, "Caller detected (%d:%02d)\n", hr, min);
  87.     processMail = checkNegMail = FALSE;
  88.  
  89.     if (!called_stabilize()) return ;
  90.  
  91.     splitF(netLog, "Caller stabilized\n");
  92.  
  93.     getId();
  94.     if (!haveCarrier) return;
  95.  
  96.     rcvStuff(FALSE);
  97.  
  98.     splitF(netLog, "Networking finished, terminating call from %s\n\n",
  99.                callerName);
  100.     modStat = haveCarrier = FALSE;
  101.     interpret(cfg.pHangUp);
  102.     doResults();
  103. }
  104.  
  105. /************************************************************************/
  106. /*    rcvStuff() receive stuff, like it says.             */
  107. /************************************************************************/
  108. rcvStuff(reversed)
  109. char reversed;
  110. {
  111.     label        tempNm;
  112.     struct cmd_data cmds;
  113.  
  114.     zero_struct(cmds);
  115.  
  116.     do {
  117.     getNextCommand(&cmds);
  118.     switch (cmds.command) {
  119.         case HANGUP:                    break;
  120.         case NORMAL_MAIL:    getMail(&cmds);         break;
  121.         case A_FILE_REQ:
  122.         case R_FILE_REQ:    netFileReq(&cmds);        break;
  123.         case NET_ROOM:    netRoomReq(&cmds);        break;
  124.         case ROLE_REVERSAL: reqReversal(&cmds, reversed);    break;
  125.         case CHECK_MAIL:    reqCheckMail(&cmds);        break;
  126.         default:        sPrintf(tempNm, "'%d' unknown.", cmds.command);
  127.                 reply(BAD, tempNm);        break;
  128.     }
  129.     } while (gotCarrier() && cmds.command != HANGUP);
  130. }
  131.  
  132. /************************************************************************/
  133. /*    doResults() processes results of receiving thingies and such.    */
  134. /************************************************************************/
  135. doResults()
  136. {
  137.     int inMail();
  138.  
  139.     if (processMail)        readMail(TRUE, inMail);
  140.     if (callSlot == ERROR)    /* If didn't know this node, don't    */
  141.     return ;        /* bother with anything else        */
  142.     if (checkNegMail)        readNegMail();
  143.     doNetRooms();
  144.     putNet(thisNet);
  145. }
  146.  
  147. /************************************************************************/
  148. /*    getId() Gets nodeId and nodeName from caller.            */
  149. /************************************************************************/
  150. getId()
  151. {
  152.     int  increment();
  153.     int  i;
  154.     char *secRunner;
  155.  
  156.     if (!haveCarrier) return;
  157.     counter = 0;
  158.     readFile(increment);
  159.     if (!gotCarrier()) return modStat = haveCarrier = FALSE;
  160.     strCpy(callerId, sectBuf);
  161.     secRunner = sectBuf;
  162.     while (*secRunner != 0) secRunner++;
  163.     secRunner++;
  164.     strCpy(callerName, secRunner);
  165.     normId(callerId, normed);
  166.     if ((callSlot = searchNet(normed)) == ERROR) {
  167.     sPrintf(msgBuf.mbtext, "New caller: %s (%s)", callerName, callerId);
  168.     aideMessage(FALSE);
  169.     }
  170.     else dumpNodeRoom(TRUE);
  171.  
  172.     splitF(netLog, "Call from %s (%s)\n", callerName, callerId);
  173. }
  174.  
  175. /************************************************************************/
  176. /*    getNextCommand() Gets next command from caller            */
  177. /************************************************************************/
  178. getNextCommand(cmds)
  179. struct cmd_data *cmds;
  180. {
  181.     int increment();
  182.  
  183.     counter = 0;
  184.     readFile(increment);
  185.     if (!gotCarrier()) return modStat = haveCarrier = FALSE;
  186.     grabCommand(cmds, sectBuf);
  187. }
  188.  
  189. /************************************************************************/
  190. /*    grabCommand() Pulls network cmds out of specified buffer    */
  191. /************************************************************************/
  192. grabCommand(cmds, sect)
  193. char *sect;
  194. struct cmd_data *cmds;
  195. {
  196.     int fcount = 0;
  197.  
  198.     cmds->command = sect[0];
  199.     sect++;
  200.     while (sect[0] > 0 && fcount < 4) {
  201.     strCpy(cmds->fields[fcount++], sect);
  202.     while (*sect != 0) sect++;
  203.     sect++;
  204.     }
  205. }
  206.  
  207. /************************************************************************/
  208. /*    reply() Replies to caller                    */
  209. /************************************************************************/
  210. reply(state, reason)
  211. char state;
  212. char *reason;
  213. {
  214.     if (!doWC(STARTUP)) {
  215.     no_good("Couldn't send reply to %s!", TRUE);
  216.     modStat = haveCarrier = FALSE;
  217.     interpret(cfg.pHangUp);
  218.     return;
  219.     }
  220.     sendWCChar(state);
  221.     if (state == BAD) {
  222.     mWCprintf("%s", reason);
  223.     if (cfg.debug) splitF(netLog, "Replying BAD: %s\n", reason);
  224.     }
  225.     sendWCChar(0);
  226.     doWC(FINISH);
  227. }
  228.  
  229. /************************************************************************/
  230. /*    reqReversal() reverses roles of networking            */
  231. /************************************************************************/
  232. reqReversal(cmds, reversed)
  233. char reversed;
  234. struct cmd_data *cmds;
  235. {
  236.     splitF(netLog, "Role reversal\n");
  237.  
  238.     if (reversed) {
  239.     reply(BAD, "Synch error on Reversal!");
  240.     return ;
  241.     }
  242.  
  243.     reply(GOOD, "");
  244.     if (callSlot == ERROR)    /* Forces a "null" role reversal    */
  245.     zero_struct(netBuf.nbflags);
  246.  
  247.     sendStuff(TRUE);
  248. }
  249.  
  250. /************************************************************************/
  251. /*    reqCheckMail() does negative acks on netMail            */
  252. /************************************************************************/
  253. reqCheckMail(cmds)
  254. struct cmd_data *cmds;
  255. {
  256.     int targetCheck();
  257.  
  258.     if (!processMail) {
  259.     reply(BAD, "No mail to check!");
  260.     return ;
  261.     }
  262.  
  263.     reply(GOOD, "");
  264.     splitF(netLog, "checking Mail\n");
  265.  
  266.     if (doWC(STARTUP)) {
  267.     readMail(FALSE, targetCheck);
  268.     sendWCChar(NO_ERROR);
  269.     doWC(FINISH);
  270.     }
  271. }
  272.  
  273. /************************************************************************/
  274. /*    targetCheck() checks for existence of recipients        */
  275. /************************************************************************/
  276. targetCheck()
  277. {
  278.     char         sigChar;
  279.     int          logNo;
  280.     struct logBuffer lBuf;
  281.  
  282.     if (!tempMess.mbto[0]) {        /* Tsk, tsk!!! */
  283.     sigChar = BAD_FORM;
  284.     }
  285.     else {
  286.     logNo = findPerson(tempMess.mbto, &lBuf);
  287.     if (logNo == ERROR && hash(tempMess.mbto) != hash("Sysop")) {
  288.         sigChar = NO_RECIPIENT;
  289.     }
  290.     else return ;
  291.     }
  292.     sendWCChar(sigChar);
  293.     mWCprintf(tempMess.mbauth);
  294.     mWCprintf(tempMess.mbto);
  295.     mWCprintf("%s @ %s", tempMess.mbdate, tempMess.mbtime);
  296. }
  297.  
  298. /************************************************************************/
  299. /*    doNetRooms() integrates temporary files into data base        */
  300. /************************************************************************/
  301. doNetRooms()
  302. {
  303.     label tempNm;
  304.     int   rover;
  305.  
  306.     for (rover = 0; rover < SHARED_ROOMS; rover++) {
  307. /*splitF(netLog, "doNetRooms(): %d\n", rover);*/
  308.     if (chkNeedsProcessing(rover)) {
  309. /*splitF(netLog, "1\n");*/
  310.         sPrintf(tempNm, netRoomTemplate, cfg.netDisk + 'a',
  311.                               netRoomSlot(rover));
  312. /*splitF(netLog, "2: -%s-\n", tempNm);*/
  313. #ifdef NET_BUG
  314.         if ((netMisc = localcheck = safeopen(tempNm, "rb")) == NULL) {
  315. #else
  316.         if ((netMisc = safeopen(tempNm, "rb")) == NULL) {
  317. #endif
  318. /*splitF(netLog, "2.a\n");*/
  319. check();
  320.         no_good("Couldn't open temproom file from %s.", TRUE);
  321. /*splitF(netLog, "2.b\n");*/
  322. check();
  323.         return;
  324.         }
  325. /*splitF(netLog, "3\n");*/
  326. check();
  327.         getRoom(netRoomSlot(rover));
  328. /*splitF(netLog, "4: thisRoom=%d\n", thisRoom);*/
  329. check();
  330. /*splitF(netLog, "5\n");*/
  331. check();
  332.         while (getNetMessage()) {
  333. /*splitF(netLog, "6: loop\n");*/
  334. check();
  335.         if (strCmpU(cfg.nodeId + cfg.codeBuf, tempMess.mborig)
  336.                                 != SAMESTRING) {
  337. /*splitF(netLog, "6.1\n");*/
  338. check();
  339.             putNetMessage();
  340. /*splitF(netLog, "6.2\n");*/
  341. check();
  342.             noteMessage(NULL, ERROR);
  343. /*splitF(netLog, "6.3\n");*/
  344. check();
  345.         }
  346. /*splitF(netLog, "7\n");*/
  347. check();
  348.         }
  349. /*splitF(netLog, "8\n");*/
  350. check();
  351.         fclose(netMisc);
  352. /*splitF(netLog, "9\n");*/
  353.         unlink(tempNm);
  354. /*splitF(netLog, "10\n");*/
  355.         resetNeedsProcessing(rover);
  356. /*splitF(netLog, "11\n");*/
  357.     }
  358.     }
  359. }
  360.  
  361. #ifdef NET_BUG
  362. check()
  363. {
  364.     if (localcheck != netMisc)
  365.         splitF(netLog, "DISASTER!\n");
  366. }
  367. #endif
  368.  
  369. /************************************************************************/
  370. /*    getMail() Grabs mail from caller                */
  371. /************************************************************************/
  372. getMail(cmds)
  373. struct cmd_data *cmds;
  374. {
  375.     label tempNm;
  376.     int   putFLChar();
  377.  
  378.     splitF(netLog, "Receiving mail from %s\n", callerName);
  379.     reply(GOOD, "");
  380.     strCpy(tempNm, "a:tempmail.$$$");
  381.     tempNm[0]  = cfg.netDisk + 'a';
  382.     if ((upfd = safeopen(tempNm, "wrb")) == NULL) {
  383.     crashout("Couldn't create networker temporary file!");
  384.     }
  385.     readFile(putFLChar);
  386.     fclose(upfd);
  387.     processMail = TRUE;
  388. }
  389.  
  390. /************************************************************************/
  391. /*    netFileReq() Handles request for file transfer            */
  392. /************************************************************************/
  393. netFileReq(cmds)
  394. struct cmd_data *cmds;
  395. {
  396.     int  netMultiSend();
  397.     int  roomSlot;
  398.     int  data;
  399.     char reason[50];
  400.     FILE *fd;
  401.  
  402.     splitF(netLog, "\nFile request: %s in %s\n", cmds->fields[1],
  403.  cmds->fields[0]);
  404.  
  405.     if ((roomSlot = roomExists(cmds->fields[0])) == ERROR   ||
  406.             roomTab[roomSlot].rtflags.MSDOSDIR != 1 ||
  407.             roomTab[roomSlot].rtflags.DOWNLOAD != 1) {
  408.     sPrintf(reason, "Room %s does not exist.", cmds->fields[0]);
  409.     if (cfg.debug) splitF(netLog, "roomSlot=%d,DIR=%d,DOWNLOAD=%d\n",
  410.             roomSlot,
  411.             roomTab[roomSlot].rtflags.MSDOSDIR,
  412.             roomTab[roomSlot].rtflags.DOWNLOAD);
  413.     reply(BAD, reason);
  414.     return;
  415.     }
  416.  
  417.     getRoom(roomSlot);
  418.  
  419.     if (cmds->command == A_FILE_REQ) {
  420.     reply(GOOD, "");
  421.     sPrintf(msgBuf.mbtext, "Following files sent to %s from %s: ",
  422.              callerName, roomBuf.rbname);
  423.     wildCard(netMultiSend, cmds->fields[1]);
  424.     if (doWC(STARTUP)) {
  425.         mWCprintf("");
  426.         doWC(FINISH);
  427.     }
  428.     }
  429.     else {
  430.     if (!setSpace(roomBuf.rbdisk, roomBuf.rbdirname)) {
  431.         sPrintf(reason, "Couldn't open directory for %s.", cmds->fields[0]);
  432.         reply(BAD, reason);
  433.         return;
  434.     }
  435.  
  436.     if ((fd = safeopen(cmds->fields[1], "rb")) == NULL) {
  437.         sPrintf(reason, "There is no '%s' in %s.", cmds->fields[1],
  438.                                cmds->fields[0]);
  439.         reply(BAD, reason);
  440.         if (strLen(roomBuf.rbdirname)!= 0) setSpace(roomBuf.rbdisk, dotdot);
  441.         setSpace(cfg.homeDisk, "");
  442.         return;
  443.     }
  444.  
  445.     reply(GOOD, "");
  446.     sendWCFile(fd);
  447.     sPrintf(msgBuf.mbtext,
  448.         "%s downloaded from %s during networking by %s.",
  449.         cmds->fields[1], roomBuf.rbname, callerName);
  450.     if (strLen(roomBuf.rbdirname) != 0) setSpace(roomBuf.rbdisk, dotdot);
  451.     setSpace(cfg.homeDisk, "");
  452.     }
  453.  
  454.     aideMessage(FALSE);
  455. }
  456.  
  457. /************************************************************************/
  458. /*    netRoomReq() Handles request for room networking        */
  459. /************************************************************************/
  460. netRoomReq(cmds)
  461. struct cmd_data *cmds;
  462. {
  463.     int slot;
  464.     char reason[80];
  465.     int  arraySlot;
  466.  
  467.     if ((slot = roomExists(cmds->fields[0])) == ERROR) {
  468.     sPrintf(reason, "'%s' does not exist", cmds->fields[0]);
  469.     reply(BAD, reason);
  470.     return ;
  471.     }
  472.  
  473.     if (roomTab[slot].rtflags.SHARED == 0) {
  474.     sPrintf(reason, "'%s' is not a networking room", cmds->fields[0]);
  475.     reply(BAD, reason);
  476.     return ;
  477.     }
  478.  
  479.     if (callSlot == ERROR || (arraySlot = chkSharing(slot)) == ERROR) {
  480. splitF(netLog, "BAD: callSlot=%d, arraySlot=%d\n", callSlot, arraySlot);
  481.     sPrintf(reason, "'%s' is not networking with you", cmds->fields[0]);
  482.     reply(BAD, reason);
  483.     return ;
  484.     }
  485.  
  486.     reply(GOOD, "");
  487.     sPrintf(reason, netRoomTemplate, cfg.netDisk + 'a', slot);
  488.     upfd = safeopen(reason, "wb");
  489.     splitF(netLog, "Receiving %s\n", cmds->fields[0]);
  490.     readFile(putFLChar);
  491.     fclose(upfd);
  492.     setNeedsProcessing(arraySlot);  /* Set flag indicating room has netted*/
  493. /*splitF(netLog, "After processing %s:\n\n", cmds->fields[0]);*/
  494. dumpNodeRoom(TRUE);
  495. }
  496.  
  497. /************************************************************************/
  498. /*    chkSharing() See if given room slot is networking with system    */
  499. /*             that has just called                */
  500. /************************************************************************/
  501. chkSharing(i)
  502. int i;
  503. {
  504.     int rover;
  505.  
  506.     for (rover = 0; rover < SHARED_ROOMS; rover++) {
  507.     if (netBuf.netRooms[rover].srgen & 0x8000 &&
  508.                  netRoomSlot(rover) == i &&
  509.                  netGen(rover) == roomTab[i].rtgen)
  510.         return rover;
  511.     }
  512.     return ERROR;
  513. }
  514.  
  515. /************************************************************************/
  516. /*    netMultiSend() Send files via WC                */
  517. /************************************************************************/
  518. netMultiSend(fn)
  519. char *fn;
  520. {
  521.     FILE *fd;
  522.     long fseek(), size, Sectors;
  523.  
  524.     if (!gotCarrier()) return ;
  525.     if ((fd = safeopen(fn, "rb")) == NULL) {
  526.     movmem(&msgBuf, &tempMess, sizeof msgBuf);
  527.     sPrintf(msgBuf.mbtext, "Network error: Couldn't find requested file %s",
  528.                     fn);
  529.     movmem(&tempMess, &msgBuf, sizeof msgBuf);
  530.     return;
  531.     }
  532.  
  533.     strCat(msgBuf.mbtext, fn);
  534.     strCat(msgBuf.mbtext, " ");
  535.     size    = (fseek(fd, -1l, 2)+1);
  536.     Sectors    = ((size + 127) / SECTSIZE);
  537.     rewind(fd);
  538.     if (doWC(STARTUP)) {
  539.     mWCprintf("%s", fn);
  540.     mWCprintf("%ld", Sectors);
  541.     doWC(FINISH);
  542.     }
  543.     sendWCFile(fd);
  544. }
  545.